package com.zp.base.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.base.entity.bo.PerformFreqDictBo;
import com.zp.base.entity.vo.PerformFreqDictVo;
import com.zp.base.service.IPerformFreqDictService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 执行频次控制器
 * 前端访问路由地址为:/base/performFreqDict
 *
 * @author zhang peng
 * @date 2022-12-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/performFreqDict")
public class PerformFreqDictController extends BaseController {

    private final IPerformFreqDictService iPerformFreqDictService;

    /**
     * 查询执行频次列表 (分页)
     */
    @SaCheckPermission("base:performFreqDict:list")
    @GetMapping("/selectPage")
    public TableDataInfo<PerformFreqDictVo> selectPage (PerformFreqDictBo bo, PageQuery pageQuery) {
        return iPerformFreqDictService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询执行频次列表 (不分页)
     */
    @SaCheckPermission("base:performFreqDict:list")
    @GetMapping("/list")
    public R<List<PerformFreqDictVo>> list (PerformFreqDictBo bo) {
        List<PerformFreqDictVo> list = iPerformFreqDictService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出执行频次列表
     */
    @SaCheckPermission("base:performFreqDict:export")
    @Log(title = "执行频次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (PerformFreqDictBo bo, HttpServletResponse response) {
        List<PerformFreqDictVo> list = iPerformFreqDictService.queryList(bo);
        ExcelUtil.exportExcel(list, "执行频次", PerformFreqDictVo.class, response);
    }

    /**
     * 获取执行频次详细信息
     *
     * @param freqId 主键
     */
    @SaCheckPermission("base:performFreqDict:query")
    @GetMapping("/{freqId}")
    public R<PerformFreqDictVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String freqId) {
        return R.ok(iPerformFreqDictService.queryById(freqId));
    }

    /**
     * 新增执行频次
     */
    @SaCheckPermission("base:performFreqDict:add")
    @Log(title = "执行频次", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody PerformFreqDictBo bo) {
        return toAjax(iPerformFreqDictService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改执行频次
     */
    @SaCheckPermission("base:performFreqDict:edit")
    @Log(title = "执行频次", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody PerformFreqDictBo bo) {
        return toAjax(iPerformFreqDictService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除执行频次
     *
     * @param freqIds 主键串
     */
    @SaCheckPermission("base:performFreqDict:remove")
    @Log(title = "执行频次", businessType = BusinessType.DELETE)
    @DeleteMapping("/{freqIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] freqIds) {
        return toAjax(iPerformFreqDictService.deleteWithValidByIds(Arrays.asList(freqIds), true) ? 1 : 0);
    }
}
